package br.com.util;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.*;

public class JPAUtil implements Serializable {

    public static EntityManagerFactory factory;

    public static EntityManagerFactory retornarFabrica() {
        if (factory == null) {
            factory = Persistence.createEntityManagerFactory("PafEcf");
        }
        return factory;
    }

    public static void persistirObjeto(Object obj) {
        EntityManager em = retornarFabrica().createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        em.merge(obj);
        tx.commit();
        em.close();
    }

    public static Object mergeObjeto(Object obj) {
        EntityManager em = retornarFabrica().createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        obj = em.merge(obj);
        tx.commit();
        em.close();
        return obj;
    }

    public static void delete(Object obj) {
        EntityManager em = retornarFabrica().createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        obj = em.merge(obj);
        em.remove(obj);
        tx.commit();
        em.close();
    }
    //salvando em rede(CAIXAS)

    public static Object mergeObjetoCaixa(String ip, Object obj) {
        EntityManager em = retornaFabricaDinamico(ip);
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        obj = em.merge(obj);
        tx.commit();
        em.close();
        return obj;
    }

    public static void deleteCaixa(String ip, Object obj) {
        EntityManager em = retornaFabricaDinamico(ip);
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        obj = em.merge(obj);
        em.remove(obj);
        tx.commit();
        em.close();
    }

    public static EntityManager retornaFabricaDinamico(String ip) {
        EntityManagerFactory emf;
        EntityManager em;
        Map prop = new HashMap();
        prop.put("hibernate.connection.url", "jdbc:mysql://" + ip + ":3306/t2tipafecfjava");
        emf = Persistence.createEntityManagerFactory("PafEcf", prop);
        em = emf.createEntityManager();
        return em;
    }

    public static Connection conectaBanco(String ip) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://" + ip + ":3306/t2tipafecfjava?user=root&password=root";
            Connection con = DriverManager.getConnection(url);
            return con;
        } catch (Exception e) {
            return null;
        }
    }

    public static String CaminhoRelatorio() {
        return "/var/www/vhosts/sistemaemjava.com.br/appservers/upload/";
    }

    public static List buscarTodos(Class clazz) {
        EntityManager em = JPAUtil.retornarFabrica().createEntityManager();
        Query q = em.createQuery("FROM " + clazz.getName());
        return q.getResultList();
    }

    public static <T> T buscarPorId(Class clazz, Integer id) {
        EntityManager em = JPAUtil.retornarFabrica().createEntityManager();
        return (T) em.find(clazz, id);

    }
}